import { Component, ViewChild, ElementRef } from '@angular/core';

import { NavController } from 'ionic-angular';
import { ProductDetailsPage } from '../product-details/product-details';
import { ScreeningPage } from '../screening/screening';
import { NavtiveServeProvider } from '../../providers/navtiveServe/navtiveServe';
import { QrScannerPage } from '../mine/qrScanner/qrScanner';
import { UrlConstant } from '../../commons/url';
import { HttpserveProvider } from '../../providers/httpserve/httpserve';


@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  num = 1;
  hide: boolean = true;
  list;
  @ViewChild('audio') audio: ElementRef
  constructor(
    public navCtrl: NavController,
    private navtive: NavtiveServeProvider,
    private http: HttpserveProvider,
    private url: UrlConstant,
  ) {
    this.init();
    let parmars = { till: 10 }
    this.http.post(this.url.ProductsList, parmars)
      .subscribe(data=>{

        console.log(data);
        
      },err=>{
        console.log(err);
        
      })
  }

  init() {
    this.list = [
      { imgSrc: "https://ss0.bdstatic.com/6ONWsjip0QIZ8tyhnq/it/u=1618097094,4154452434&fm=77&w_h=121_75&cs=423647557,799948659", "title": "001-aaa", "price": "6 US", "num": 1 },
      { imgSrc: "https://ss0.bdstatic.com/6ONWsjip0QIZ8tyhnq/it/u=1618097094,4154452434&fm=77&w_h=121_75&cs=423647557,799948659", "title": "002-aaa", "price": "6 US", "num": 1 },
      { imgSrc: "https://ss0.bdstatic.com/6ONWsjip0QIZ8tyhnq/it/u=1618097094,4154452434&fm=77&w_h=121_75&cs=423647557,799948659", "title": "003-aaa", "price": "6 US", "num": 1 },
      { imgSrc: "https://ss0.bdstatic.com/6ONWsjip0QIZ8tyhnq/it/u=1618097094,4154452434&fm=77&w_h=121_75&cs=423647557,799948659", "title": "001-aaa", "price": "6 US", "num": 1 },
      { imgSrc: "https://ss0.bdstatic.com/6ONWsjip0QIZ8tyhnq/it/u=1618097094,4154452434&fm=77&w_h=121_75&cs=423647557,799948659", "title": "001-aaa", "price": "6 US", "num": 1 },
      { imgSrc: "https://ss0.bdstatic.com/6ONWsjip0QIZ8tyhnq/it/u=1618097094,4154452434&fm=77&w_h=121_75&cs=423647557,799948659", "title": "001-aaa", "price": "6 US", "num": 1 },
      { imgSrc: "https://ss0.bdstatic.com/6ONWsjip0QIZ8tyhnq/it/u=1618097094,4154452434&fm=77&w_h=121_75&cs=423647557,799948659", "title": "001-aaa", "price": "6 US", "num": 1 },
      { imgSrc: "https://ss0.bdstatic.com/6ONWsjip0QIZ8tyhnq/it/u=1618097094,4154452434&fm=77&w_h=121_75&cs=423647557,799948659", "title": "001-aaa", "price": "6 US", "num": 1 },
      { imgSrc: "https://ss0.bdstatic.com/6ONWsjip0QIZ8tyhnq/it/u=1618097094,4154452434&fm=77&w_h=121_75&cs=423647557,799948659", "title": "001-aaa", "price": "6 US", "num": 1 },
      { imgSrc: "https://ss0.bdstatic.com/6ONWsjip0QIZ8tyhnq/it/u=1618097094,4154452434&fm=77&w_h=121_75&cs=423647557,799948659", "title": "001-aaa", "price": "6 US", "num": 1 },
      { imgSrc: "https://ss0.bdstatic.com/6ONWsjip0QIZ8tyhnq/it/u=1618097094,4154452434&fm=77&w_h=121_75&cs=423647557,799948659", "title": "001-aaa", "price": "6 US", "num": 1 },
      { imgSrc: "https://ss0.bdstatic.com/6ONWsjip0QIZ8tyhnq/it/u=1618097094,4154452434&fm=77&w_h=121_75&cs=423647557,799948659", "title": "001-aaa", "price": "6 US", "num": 1 },
    ]
    
  }

  //筛选
  getItems(ev) {
    this.init();
    var val = ev.target.value;
    if (val && val.trim() != '') {
      this.list = this.list.filter((item) => {
        return ((item.title).toLowerCase().indexOf(val.toLowerCase()) > -1);
      })
    }
  }
  // 条件筛选
  screen() {
    this.navCtrl.push(ScreeningPage);
  }
  // 跳扫一扫页面
  qrScanne() {
    this.navCtrl.push(QrScannerPage);
  }
  //点击加入购物车听到声音
  bofang($event, item) {
    $event.stopPropagation();
    let myAudio = this.audio.nativeElement;
    myAudio.load();
    myAudio.play();
    //this.hide = false;
    item.visibleBtn = true;
  }
  //跳转详情页
  jumpDetail($event: Event, i, item) {
    $event.stopPropagation();
    this.navCtrl.push(ProductDetailsPage, {
      item: item
    })
  }
  //组织冒泡
  stop($event: Event) {
    $event.stopPropagation();
  }
  //购物车加减
  incNum(item, num) {
    if (!item.num) {
      item.num = 1;
    }
    item.num += num;
    if (item.num < 1) {
      item.num = 1;
    }
  }
  //加入购物车
  doDelete($event: Event, i) {
    $event.stopPropagation();
  }

}
